package main.request;

import java.io.IOException;
import java.net.Socket;

import utils.Debug;
import main.process.FtpRequest;

public class RequestPort implements ProcessRequestInterface {

	private static class SingletonHolder
	{		
		/** Instance unique non préinitialisée */
		private final static RequestPort instance = new RequestPort();
	}
	
	public static RequestPort getInstance()
	{
		return SingletonHolder.instance;
	}
	
	@Override
	public void processRequest(String[] request, FtpRequest ftp) throws IOException {
		Debug.debug(this.getClass().getName(), request, ftp.getDebug());
		
		if(!request[0].equals("PORT"))
		{
			next(request, ftp);
		}
		else
		{
			String client[] = request[1].split(",");
			String address = client[0] + "." + client[1] + "." + client[2] + "." + client[3];
			int port = (Integer.parseInt(client[4]) * 256) + Integer.parseInt(client[5]);
			ftp.setDataSocket(new Socket(address,port));	
			ftp.write(200 + " PORT ok");
			ftp.setPassive(false);
		}
	}

//	@Override
	public void next(String[] request, FtpRequest ftp) throws IOException {
		RequestPwd.getInstance().processRequest(request, ftp);

	}

}
